package cn.endeavour.pet.service.impl;

import cn.endeavour.basic.exception.BusinessException;
import cn.endeavour.basic.service.impl.BaseServiceImpl;
import cn.endeavour.basic.util.*;
import cn.endeavour.order.domain.PetAcquisitionOrder;
import cn.endeavour.order.mapper.PetAcquisitionOrderMapper;
import cn.endeavour.org.domain.Employee;
import cn.endeavour.org.domain.Shop;
import cn.endeavour.org.mapper.EmployeeMapper;
import cn.endeavour.org.mapper.ShopMapper;
import cn.endeavour.pet.domain.Pet;
import cn.endeavour.pet.domain.SearchMasterMsg;
import cn.endeavour.pet.mapper.SearchMasterMsgMapper;
import cn.endeavour.pet.query.SearchMasterMsgQuery;
import cn.endeavour.pet.service.IPetService;
import cn.endeavour.pet.service.ISearchMasterMsgService;
import cn.endeavour.user.domain.LoginInfo;
import cn.endeavour.user.domain.User;
import cn.endeavour.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private IPetService petService;

    @Autowired
    private PetAcquisitionOrderMapper petAcquisitionOrderMapper;

    @Override
    public List<SearchMasterMsg> getAll() {
        return searchMasterMsgMapper.loadAll();
    }

    @Override
    public void publish(SearchMasterMsg searchMasterMsg, HttpServletRequest request) {
        // 校验空值
        if (StringUtils.isEmpty(searchMasterMsg.getTitle()) || StringUtils.isEmpty(searchMasterMsg.getAddress())) {
            throw new BusinessException("信息不能为空");
        }
        // 获取用户的id
        LoginInfo loginInfo = LoginContextUtil.getLoginInfo(request);
        Long id = loginInfo.getId();
        // 查询用户id
        User user = userMapper.loadByLoginInfoId(id);
        searchMasterMsg.setUser(user);
        searchMasterMsg.setUser_id(user.getId());
        // 查询最近的店铺,先得到坐标值
        Point point = DistanceUtil.getPoint(searchMasterMsg.getAddress());
        Shop nearestShop = DistanceUtil.getNearestShop(point, shopMapper.loadAll());
        // 设置店铺id
        searchMasterMsg.setShop_id(nearestShop.getId());
        searchMasterMsg.setShop(nearestShop);
        // 保存在数据库中
        searchMasterMsgMapper.save(searchMasterMsg);
        // 给店铺发送短信
        Employee employee = employeeMapper.loadById(nearestShop.getAdmin_id());
        // 给管理员发送短信
//        SmsUtils.sendSms(employee.getPhone(), "您有新的生意来了,请及时处理")
        System.out.println(employee.getPhone() + "您有新的生意来了,请及时处理");

    }

    @Override
    public PageList<SearchMasterMsg> processed(SearchMasterMsgQuery query, HttpServletRequest request) {
        // 查询待处理信息
        query.setState(1);
        // 判断是店铺管理员还是平台管理员
        LoginInfo loginInfo = LoginContextUtil.getLoginInfo(request);
        // 通过登录信息表查询员工表
        Employee employee = employeeMapper.findByLoginInfoId(loginInfo.getId());
        if (employee.getShop_id() != null) {
            // 设置店铺id
            query.setShop_id(employee.getShop_id());
        }
        return super.queryPage(query);
    }

    @Override
    public PageList<SearchMasterMsg> pending(SearchMasterMsgQuery query, HttpServletRequest request) {
        // 查询待处理信息
        query.setState(0);
        // 判断是店铺管理员还是平台管理员
        LoginInfo loginInfo = LoginContextUtil.getLoginInfo(request);
        // 通过登录信息表查询员工表
        Employee employee = employeeMapper.findByLoginInfoId(loginInfo.getId());
        if (employee.getShop_id() != null) {
            // 设置店铺id
            query.setShop_id(employee.getShop_id());
        }
        return super.queryPage(query);
    }

    @Override
    public PageList<SearchMasterMsg> user(SearchMasterMsgQuery query, HttpServletRequest request) {
        // 得到登录信息
        LoginInfo loginInfo = LoginContextUtil.getLoginInfo(request);
        User user = userMapper.loadByLoginInfoId(loginInfo.getId());
        query.setUser_id(user.getId());
        return super.queryPage(query);
    }

    @Override
    @Transactional
    public void handle(Pet pet, HttpServletRequest request) {
        // 修改宠物状态
        Long search_master_msg_id = pet.getSearch_master_msg_id();
        // 去修改状态
        searchMasterMsgMapper.updateByMsgId(search_master_msg_id);
        // 添加宠物信息
        petService.add(pet);
        // 获取到员工的id
        LoginInfo loginInfo = LoginContextUtil.getLoginInfo(request);
        Employee employee = employeeMapper.findByLoginInfoId(loginInfo.getId());
        // 查询发布寻主消息的内容
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.loadById(search_master_msg_id);
        // 转换对象,得到宠物领养订单
        PetAcquisitionOrder petAcquisitionOrder = pet2order(pet, searchMasterMsg, employee);
        petAcquisitionOrderMapper.save(petAcquisitionOrder);

        // 支付订单@TODO
    }

    private PetAcquisitionOrder pet2order(Pet pet, SearchMasterMsg adopt, Employee employee) {
        PetAcquisitionOrder order = new PetAcquisitionOrder();
        order.setDigest("[摘要]对" + pet.getName() + "收购订单！");
        order.setState(0);//待支付
        order.setPrice(pet.getCostPrice());
        order.setAddress(adopt.getAddress());
        String orderSn = CodeGenerateUtils.generateOrderSn(adopt.getUser_id());
        order.setOrderSn(orderSn);
        order.setPet_id(pet.getId());
        order.setUser_id(adopt.getUser_id());
        order.setPayType(pet.getPayType());
        order.setShop_id(pet.getShop_id());
        order.setEmp_id(employee.getId());
        return order;
    }

}
